/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.addressBook.controller;

import com.addressBook.bean.Groups;
import com.addressBook.bean.UserInfo;
import com.addressBook.util.AllUtil;
import com.fxexperience.javafx.animation.FlipInYTransition;
import com.fxexperience.javafx.animation.FlipOutYTransition;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;
import java.util.ResourceBundle;
import javafx.animation.Transition;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;

/**
 * FXML Controller class
 *
 * @author Administrator
 */
public class GroupController implements Initializable {

    private static GroupController instance;

    public static GroupController getGroupController() {
        return instance;
    }
    ArrayList<String> colorTable = new ArrayList<String>() {
        {
            add("#008287");
            add("#16499a");
            add("#4390df");
            add("#60a917");
            add("#fa6800");
            add("#9a1616");
        }
    };
    @FXML
    AnchorPane groupPane;
    @FXML
    AnchorPane group2list;
    @FXML
    VBox pageBox;
    @FXML
    HBox groupBox1;
    @FXML
    HBox groupBox2;
    @FXML
    HBox groupBox3;
    @FXML
    ImageView back;
    @FXML
    Label text;
//    @FXML
//    private 

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        instance = this;
        back.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UIController uc = UIController.getUIController();
                Transition trans = new FlipOutYTransition(uc.groupPanel);
                trans.setOnFinished(new EventHandler<ActionEvent>() {

                    @Override
                    public void handle(ActionEvent event) {
                        uc.groupPanel.setVisible(false);
                        uc.back2main();

                    }
                });
                trans.play();

            }
        });
    }

    public void pagingForG(int nowpageInG, int pageInG) {
        ArrayList<Groups> list = AllUtil.group;

        int k = (nowpageInG - 1) * 8;
        int max = nowpageInG * 8;
        if (pageInG == nowpageInG) {
            max = list.size();
//            System.out.println(max);
        }
        for (int i = 0; i < 8; i++) {
            Groups group = list.get(k);
            AnchorPane pane = new AnchorPane();
            pane.setPrefSize(260, 100);

            Label groudName = new Label();
            if (group.getGroupName().equals("")) {
                groudName.setText("默认");
            } else {
                groudName.setText(group.getGroupName());
            }
            groudName.setPrefSize(150, 30);
            groudName.setLayoutX(55);
            groudName.setLayoutY(55);
            groudName.setAlignment(Pos.CENTER);
            groudName.setStyle("-fx-text-fill:white;");

            Label groudCout = new Label(group.getCount() + "");
            groudCout.setPrefSize(150, 20);
            groudCout.setLayoutX(55);
            groudCout.setLayoutY(30);
            groudCout.setOpacity(0.8);
            groudCout.setAlignment(Pos.CENTER);
            groudCout.setFont(new Font(30));
            groudCout.setStyle("-fx-text-fill:white;");

            pane.getChildren().addAll(groudName, groudCout);
            int color = new Random().nextInt(5);
            System.out.println("color" + color);
            pane.setStyle("-fx-background-color:" + colorTable.get(color) + ";");
            pane.setOnMouseClicked(new EventHandler<MouseEvent>() {

                @Override
                public void handle(MouseEvent event) {

                    back.setVisible(false);
                    pageBox.setVisible(false);
                    text.setVisible(false);
                    Group2listController gc = Group2listController.getGroup2listController();

                    ArrayList<UserInfo> list2 = new ArrayList<UserInfo>();//保存选中分组内的人
                    ArrayList<UserInfo> list1 = AllUtil.beanArrayList;
                    for (int i = 0; i < list1.size(); i++) {
                        if (list1.get(i).getCATEGORIES().equals(group.getGroupName())) {
                            list2.add(list1.get(i));
                        }
                    }

                    gc.showUser(group, list2);
                    group2list.setOpacity(0);
                    group2list.setVisible(true);
                    new FlipInYTransition(group2list).play();
                }
            });
            if (i <= 2) {
                groupBox1.getChildren().add(pane);
                k = k + 1;
            } else if (i <= 5) {
                groupBox2.getChildren().add(pane);
                k = k + 1;
            } else if (i <= 7) {
                groupBox3.getChildren().add(pane);
                k = k + 1;
            }
            if (k >= max) {
//                System.out.println("------------");
                AnchorPane addPane = new AnchorPane();
                addPane.setPrefSize(260, 100);
                Label addLabel = new Label("新增");
//                addLabel.setLayoutX(100);
//                addLabel.setLayoutY(80);
//                Image addImage=new Image("icons/useradd.png");
//                ImageView addImageView=new ImageView(addImage);
//                addImageView.setFitHeight(80);
//                addImageView.setFitWidth(80);
//                addImageView.setLayoutX(90);
//                addImageView.setLayoutY(0);
                addLabel.setStyle("-fx-text-fill:white;");
                addPane.getChildren().add(addLabel);
                color = new Random().nextInt(5);
                addPane.setStyle("-fx-background-color:" + colorTable.get(color) + ";");
//                addPane.setOnMouseClicked(new EventHandler<MouseEvent>() {
//
//                    @Override
//                    public void handle(MouseEvent event) {
//                        AllUtil.personBean = new UserInfo();
//                        toolBar.setVisible(false);
//                        iconBar.setVisible(false);
//                        pageBox.setVisible(false);
//                        leftButton.setVisible(false);
//                        rightButton.setVisible(false);
//                        personInfoPanel.setOpacity(0);
//                        toolPanelContainer.setVisible(true);
//                        personInfoPanel.setVisible(true);
//                        new FlipInYTransition(personInfoPanel).play();
//                        PersonInfoController pc = PersonInfoController.getPersonInfoController();
//                        pc.setText();
//                    }
//                });
                if (i < 2) {
//                    System.out.println("111111111");
                    groupBox1.getChildren().add(addPane);
                    k = k + 1;
                } else if (i < 5) {
//                    System.out.println("222222222222");
                    groupBox2.getChildren().add(addPane);
                    k = k + 1;
                } else if (i < 8) {
                    groupBox3.getChildren().add(addPane);
                    k = k + 1;
                }
                break;
            }
        }
    }

    public void back() {

        groupPane.setVisible(true);
        back.setVisible(true);
        text.setVisible(true);
        pageBox.setVisible(true);

//        group2list.setVisible(false);
    }
}
